package com.shxc.smarttrade.old.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.MarketMsgBean;
import com.shxc.smarttrade.old.myinterface.MarketInformationClickListener;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/16.
 */

public class MarketInformationTwoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<MarketMsgBean.DataBean> marketMsgList;
    private LayoutInflater mInflater;
    private MarketInformationClickListener listener;

    public MarketInformationTwoAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);

    }

    public void setDatas(List<MarketMsgBean.DataBean> marketMsgList) {
        this.marketMsgList = marketMsgList;
        notifyDataSetChanged();
    }


    public void setMarketInforItemCLickListener(MarketInformationClickListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new MarkHolder(mInflater.inflate(R.layout.adapter_market_infomation, parent, false));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MarkHolder) {
            if (position == 0)
                ((MarkHolder) holder).tvTopLine.setVisibility(View.INVISIBLE);
            else
                ((MarkHolder) holder).tvTopLine.setVisibility(View.VISIBLE);

            final MarketMsgBean.DataBean msb = marketMsgList.get(position);
            ((MarkHolder) holder).tvTitle.setText(msb.getSource());
            ((MarkHolder) holder).tvContent.setText(msb.getTitle());
            ((MarkHolder) holder).tvTime.setText(BaseUtils.friendlyTime(msb.getTime()));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemCLick(position, msb);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return marketMsgList == null ? 0 : marketMsgList.size();
    }


    public class MarkHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_topLine)
        TextView tvTopLine;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_content)
        TextView tvContent;

        MarkHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }
}
